package org.ysu.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import org.ysu.core.YsuException;
import org.ysu.entity.Admin;

import java.util.Date;

public class JwtUtil {
    private static final String KEY = "zhaojingyi";
    public static String create(Admin admin){
        return JWT.create()
                .withClaim("id",admin.getId())
                .withClaim("name",admin.getName())
                .withClaim("time",new Date())
                .sign(Algorithm.HMAC256(KEY));
    }
    public static long decode(String jwt) throws YsuException {
        try {
            return JWT.require(Algorithm.HMAC256(KEY))
                    .build()
                    .verify(jwt)
                    .getClaim("id").asLong();
        }catch (Exception ex){
            ex.printStackTrace();
            throw new YsuException("非法请求");
        }
    }
}
